package com.hectopascal.goalance.controller;

import com.hectopascal.goalance.pojo.Target;
import com.hectopascal.goalance.pojo.dto.ResponseMessage;
import com.hectopascal.goalance.service.Ipml.ITargetService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/target")
public class TargetController {
    @Autowired
    ITargetService targetService;

    @PostMapping("/addtarget")
    public ResponseMessage<Target> targetAdd(@RequestBody Target target, HttpServletRequest request) {
        target.setUserId((Integer) request.getAttribute("userId"));
        Target target1 = targetService.targetAdd(target);
        return new ResponseMessage<>(200, "success", target);
    }

    @GetMapping("/gettarget")
    public ResponseMessage<List<Target>> targetGet(@RequestParam Integer userId) {
        List<Target> targets = targetService.targetGet(userId);
        return new ResponseMessage<>(200, "success", targets);
    }

    @PutMapping("/updatetarget")
    public ResponseMessage<String> targetUpdate(@RequestBody Target target) {
        int rows = targetService.targetUpdate(target);
        if(rows>0){
            return ResponseMessage.success("修改目标信息成功！");
        }else{
            return ResponseMessage.fail("修改目标信息失败！");
        }
    }
}
